1791C - Prepend and Append - CodeForces Solution


implementation two pointers

Please click on ads to support us..

C++ Code:

#include"bits/stdc++.h"
using namespace std;
#define FIO  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define int long long
#define nl "\n"
#define ff first
#define ss second
#define pb push_back
#define w(t) int t; cin >> t; while (t--)
#define deci(n) fixed << setprecision(n)
#pragma GCC optimize ("-O3")

#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)              cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define pi 3.1415926535897932384626433832795
// replace int with int32_t
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;

const int MOD = 1e9 + 7;
int md = MOD;

const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void google(int x) {
    cout << "Case #" << x << ": ";
}

int Power(int n, int x) {
    int ans = 1; while (x > 0) {
        if (x & 1) ans = (ans * n) % md;
        n = (n * n) % md; x = x >> 1;
    } return ans;
}

vl fact, inv;

void inverse(ll n)
{   if (n >= inv.size()) {
        ll size = inv.size(); size = size == 0 ? 1 : size;
        inv.resize(n + 1); inv[0] = 1;
        for (ll i = size; i <= n; i++)inv[i] = Power(fact[i], md - 2);
    }
}

void factorial(ll n)
{   if (n >= fact.size()) {
        ll size = fact.size(); size = size == 0 ? 1 : size;
        fact.resize(n + 1); fact[0] = 1; for (ll i = size; i <= n; i++)
            fact[i] = (fact[i - 1] * i) % md;
    }
}

ll ncr(ll n, ll r) { return (((fact[n] * inv[r]) % md) * inv[n - r]) % md; }

vl SieveOfEratosthenes(int n)
{
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
    for (int p = 2; p * p <= n; p++)
    {   if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }

    vl ans; for (int p = 2; p <= n; p++)if (prime[p])ans.pb(p); return ans;
}
vi primeFactors(int n)
{
    vi ans;
    while (n % 2 == 0) {ans.pb(2); n /= 2;}
    for (int i = 3; i * i <= n; i = i + 2) {
        while (n % i == 0) {ans.pb(i); n /= i;}
    }
    if (n > 2)ans.pb(n); return ans;
}

template<typename T_vector>
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
    if (start < 0) start = 0;
    if (end < 0) end = (int)(v.size());

    for (int i = start; i < end; i++)
        cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}

//********************************************** END OF TEMPLATES************************************************//

void solve() {

    int n; cin >> n;
    string s; cin >> s;
    int i = 0, j = n - 1;
    while (i < j && s[i] != s[j]) {
        i++; j--;
    }
    cout << j - i + 1 << nl;
}

int32_t main()
{

    FIO
#ifndef ONLINE_JUDGE
    freopen ("in.txt", "r", stdin);
    freopen ("out.txt", "w", stdout);
#endif

    w(t) {
        solve();
    }

}


Comments

Submit
0 Comments
More Questions

461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List